package com.axter.libs.photo.choosephoto;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.axter.libs.base.adapter.IBaseCheckedViewHolder;
import com.axter.libs.photo.ImageLoaderCache;
import com.axter.libs.photo.R;
import com.axter.libs.photo.bean.MediaInfo;
import com.axter.libs.photo.widget.RectImageView;

/**
 * Created by zhaobo on 2016/10/14.
 */

public class ChoosePhotoViewHolderImage implements IBaseCheckedViewHolder<MediaInfo> {
    RectImageView photo_imageview;
    ImageView photo_checkbox;
    ChoosePhotoAdapter adapter;

    public ChoosePhotoViewHolderImage(ChoosePhotoAdapter adapter) {
        this.adapter = adapter;
    }

    @Override
    public Object getContentView() {
        return R.layout.choosephoto_item_image;
    }

    @Override
    public void bindViews(View view) {
        photo_imageview = (RectImageView) view.findViewById(R.id.photo_imageview);
        photo_checkbox = (ImageView) view.findViewById(R.id.photo_checkbox);
        photo_checkbox.setOnClickListener(adapter.checklistener);
    }

    @Override
    public void handleData(MediaInfo image, int position) {
        photo_checkbox.setTag(position);
        if (adapter.isChecked(position)) {
            photo_checkbox.setSelected(true);
            photo_imageview.showForegroundColor(true, false);
        } else {
            photo_checkbox.setSelected(false);
            photo_imageview.showForegroundColor(false, false);
        }
        if (!TextUtils.isEmpty(image.getThumb())) {
            ImageLoaderCache.displayLocale(image.getThumb(), photo_imageview, 200, 200, R.drawable.choosephoto_imgbg);
        } else {
            ImageLoaderCache.displayLocale(image.getPath(), photo_imageview, 200, 200, R.drawable.choosephoto_imgbg);
        }
    }

    @Override
    public void onCheckedChanged(MediaInfo mediaInfo, int position, boolean isChecked) {
        if (isChecked) {
            photo_checkbox.setSelected(true);
            photo_imageview.showForegroundColor(true, true);
        } else {
            photo_checkbox.setSelected(false);
            photo_imageview.showForegroundColor(false, true);
        }
    }
}
